package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.CustomerShare;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.service.ContractCustomerService;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class ContractCustomerServiceImpl
  implements ContractCustomerService
{
  private CustomerDAO customerDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private CustomerShareDAO customerShareDAO;
  private SaleRecordDAO saleRecordDAO;

  public CustomerDAO getCustomerDAO()
  {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public List getPageContractCustomers(Integer currentUserId, Page page, QueryCustomer queryCustomer) {
    if (currentUserId == null) return null;

    Customer customer = new Customer();
    customer.setCustomerCategory("3");

    customer.setPersonId(currentUserId);
    if (queryCustomer.getCustomerName() != null) {
      customer.setCustomerName(queryCustomer.getCustomerName());
    }
    if (queryCustomer.getSalePerson() != null)
      customer.setSalepersonName(queryCustomer.getSalePerson());
    if (queryCustomer.getAreaId() != null)
      customer.setAreaId(queryCustomer.getAreaId());
    if (queryCustomer.getIndustryId() != null) {
      customer.setIndustryId(queryCustomer.getIndustryId());
    }
    if (queryCustomer.getRequireDesc() != null) {
      customer.setRequireDesc(queryCustomer.getRequireDesc());
    }
    if (queryCustomer.getBackCostDesc() != null) {
      customer.setBackCostDesc(queryCustomer.getBackCostDesc());
    }
    if ((queryCustomer.getsContactDate() != null) && (queryCustomer.geteContactDate() != null)) {
      customer.setStartcontractDate(queryCustomer.getsContactDate());
      customer.setEndcontractDate(queryCustomer.geteContactDate());
    }
    if ((queryCustomer.getStartbackDate() != null) && (queryCustomer.getEndbackDate() != null)) {
      customer.setStartbackDate(queryCustomer.getStartbackDate());
      customer.setEndbackDate(queryCustomer.getEndbackDate());
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        Users user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }
        cCustomer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(cCustomer.getId()));

        CustomerShare customerShare = new CustomerShare();
        customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(cCustomer.getId(), currentUserId);
        if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
          cCustomer.setIsWrite(customerShare.getIsReadWrite());
        }
      }

    }

    return customers;
  }

  public Integer getPageContractCustomersCount(Integer currentUserId, QueryCustomer queryCustomer) {
    if (currentUserId == null) return null;

    Customer customer = new Customer();
    customer.setCustomerCategory("3");

    customer.setPersonId(currentUserId);
    if (queryCustomer.getCustomerName() != null) {
      customer.setCustomerName(queryCustomer.getCustomerName());
    }
    if (queryCustomer.getSalePerson() != null)
      customer.setSalepersonName(queryCustomer.getSalePerson());
    if (queryCustomer.getAreaId() != null)
      customer.setAreaId(queryCustomer.getAreaId());
    if (queryCustomer.getIndustryId() != null) {
      customer.setIndustryId(queryCustomer.getIndustryId());
    }
    if (queryCustomer.getRequireDesc() != null) {
      customer.setRequireDesc(queryCustomer.getRequireDesc());
    }
    if (queryCustomer.getBackCostDesc() != null) {
      customer.setBackCostDesc(queryCustomer.getBackCostDesc());
    }
    if ((queryCustomer.getsContactDate() != null) && (queryCustomer.geteContactDate() != null)) {
      customer.setStartcontractDate(queryCustomer.getsContactDate());
      customer.setEndcontractDate(queryCustomer.geteContactDate());
    }
    if ((queryCustomer.getStartbackDate() != null) && (queryCustomer.getEndbackDate() != null)) {
      customer.setStartbackDate(queryCustomer.getStartbackDate());
      customer.setEndbackDate(queryCustomer.getEndbackDate());
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getPageDeptContractCustomers(Integer currentUserId, Page page, QueryCustomer queryCustomer) {
    if (currentUserId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setCustomerCategory("3");
      customer.setDeptIds(deptIds);

      if (queryCustomer.getCustomerName() != null) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }
      if (queryCustomer.getSalePerson() != null)
        customer.setSalepersonName(queryCustomer.getSalePerson());
      if (queryCustomer.getAreaId() != null)
        customer.setAreaId(queryCustomer.getAreaId());
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if (queryCustomer.getRequireDesc() != null) {
        customer.setRequireDesc(queryCustomer.getRequireDesc());
      }
      if (queryCustomer.getBackCostDesc() != null) {
        customer.setBackCostDesc(queryCustomer.getBackCostDesc());
      }
      if ((queryCustomer.getsContactDate() != null) && (queryCustomer.geteContactDate() != null)) {
        customer.setStartcontractDate(queryCustomer.getsContactDate());
        customer.setEndcontractDate(queryCustomer.geteContactDate());
      }
      if ((queryCustomer.getStartbackDate() != null) && (queryCustomer.getEndbackDate() != null)) {
        customer.setStartbackDate(queryCustomer.getStartbackDate());
        customer.setEndbackDate(queryCustomer.getEndbackDate());
      }

    }

    List customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    System.out.print(customers.size());
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }
      }
    }
    return customers;
  }

  public Integer getPageDeptContractCustomersCount(Integer currentUserId, QueryCustomer queryCustomer) {
    if (currentUserId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setCustomerCategory("3");
      customer.setDeptIds(deptIds);

      if (queryCustomer.getCustomerName() != null) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }
      if (queryCustomer.getSalePerson() != null)
        customer.setSalepersonName(queryCustomer.getSalePerson());
      if (queryCustomer.getAreaId() != null)
        customer.setAreaId(queryCustomer.getAreaId());
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if (queryCustomer.getRequireDesc() != null) {
        customer.setRequireDesc(queryCustomer.getRequireDesc());
      }
      if (queryCustomer.getBackCostDesc() != null) {
        customer.setBackCostDesc(queryCustomer.getBackCostDesc());
      }
      if ((queryCustomer.getsContactDate() != null) && (queryCustomer.geteContactDate() != null)) {
        customer.setStartcontractDate(queryCustomer.getsContactDate());
        customer.setEndcontractDate(queryCustomer.geteContactDate());
      }
      if ((queryCustomer.getStartbackDate() != null) && (queryCustomer.getEndbackDate() != null)) {
        customer.setStartbackDate(queryCustomer.getStartbackDate());
        customer.setEndbackDate(queryCustomer.getEndbackDate());
      }

    }

    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }
}